<?php
/**
 * Created by PhpStorm.
 * User: jack.shan
 * Date: 2020/9/14
 * Time: 7:59
 */
namespace app\index\controller;

use app\index\BaseController;
use app\common\Helper;
use think\facade\Config;
use think\facade\Db;
use think\facade\View;
use think\facade\Filesystem;

class Code extends BaseController
{

    public function index(){

        $imgUrl = "";
        if($this->request->isPost())
        {
            // 获取表单上传文件 例如上传了001.jpg
            $file = $this->request->file('image');

            if($file){

//                validate(['image'=>[
//                          'fileSize' => 410241024,
//                          'fileExt' => 'jpg,jpeg,png,bmp,gif',
//                          'fileMime' => 'image/jpeg,image/png,image/gif', //这个一定要加上，很重要我认为！
//                     ]])->check(['image' => $file]);


                $id = rand(1,100000);
                //$picadr = Filesystem::disk('data')->putFile( 'topic', $file);
                $picadr = Filesystem::disk('data')->putFile( 'topic', $file, function() use($id){
                    return date('Ymd') . '/' . intval($id / 1000) . '/' . time();
                });

                $logo = Config::get('app.data_url') . '/' . $picadr;

                echo $logo;

                //二维码URL参数
                $text = "http://www.baidu.com";

                //二维码图片保存路径
                $pathname = dirname(__DIR__) . '/../../data/code/';
                if(!is_dir($pathname)) { //若目录不存在则创建之
                    mkdir($pathname);
                }

                $basename= Helper::code($text,$logo, $pathname);

                $imgUrl = Config::get('app.data_url') . "/code/" . $basename;
            }

        }

        View::assign([
            'imgUrl' => $imgUrl
        ]);

        return View::fetch("index");
    }

}